package com.eqdd.yiqidian.ui.mine.maker.fragment

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.bean.rebuild.LearnMineInfo
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHtmlBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditIconBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.kotlin.imgPathPreview
import com.gamerole.richtextview.RichEditor
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.fragment_personal.view.*
import java.util.*

/**
 * Created by jhj on 18-8-6.
 */
class MakerPersonalFragment : Fragment() {

    private var bean: LearnMineInfo? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_personal, container, false)
        val makerGuid = arguments?.getString("makerGuid")
        OkGo.post<HttpResult<LearnMineInfo>>(HttpConfig.BASE_URL + HttpConfig.LEARN_MINE_INFO)
                .params("userGuid", makerGuid)
                .execute(object : DialogCallBack<HttpResult<LearnMineInfo>>(activity) {
                    override fun onSuccess(response: Response<HttpResult<LearnMineInfo>>) {
                        val httpResult = response.body()
                        if (httpResult.status == 200) {
                            bean = httpResult.items
                            initView(view, bean)
                        }
                    }
                })

        return view
    }

    private fun initView(view: View, bean: LearnMineInfo?) {
        view.recyclerView.layoutManager = LinearLayoutManager(activity)
        view.recyclerView.addItemDecoration(DividerLineItemDecoration(activity))
        val slimEditBeans = arrayListOf<SlimEditBean>()
        slimEditBeans.add(SlimEditIconBean("头像", bean?.headimage, 0))
        slimEditBeans.add(SlimEditHintBean("姓名", bean?.realname, 1))
        slimEditBeans.add(SlimEditHintBean("手机", bean?.phone, 2))
        slimEditBeans.add(SlimEditHintBean("助理手机", bean?.assistantPhone, 3))
        slimEditBeans.add(SlimEditHintBean("助理姓名", bean?.assistant, 4))
        slimEditBeans.add(SlimEditHintBean("邮箱", bean?.email, 5))
        slimEditBeans.add(SlimEditHintBean("QQ", bean?.qq, 6))
        slimEditBeans.add(SlimEditHintBean("微信", bean?.wechat, 7))
        slimEditBeans.add(SlimEditHintBean("常驻地址", bean?.address, 8))
        slimEditBeans.add(SlimEditHintBean("研究领域", bean?.researchField, 9))
        slimEditBeans.add(SlimEditHintBean("课程价格(元/天)", String.format(Locale.CHINA, "%.2f元/天", bean?.cooperativePrice), 10))
        slimEditBeans.add(SlimEditHintBean("课程", bean?.courses, 11))
        slimEditBeans.add(SlimEditHintBean("授课风格", bean?.teachStyle, 12))
        slimEditBeans.add(SlimEditHtmlBean("讲师简介", bean?.lecturerBackground?.replace("\\", ""), 13))
        slimEditBeans.add(SlimEditHtmlBean("客户案例", bean?.custCase?.replace("\\", ""), 14))
        slimEditBeans.add(SlimEditHtmlBean("曾服务过的企业", bean?.serviceCom?.replace("\\", ""), 15))
        SlimAdapterEx.create()
                .register<SlimEditHintBean>(R.layout.list_item_28, SlimInjector<SlimEditHintBean> { data, injector ->
                    injector?.text(R.id.tv_left_content, data?.title)
                            ?.text(R.id.tv_right_hint_content, data?.content)
                })
                .register<SlimEditIconBean>(R.layout.list_item_23, SlimInjector<SlimEditIconBean> { data, injector ->
                    injector?.text(R.id.tv_left_content, data?.title)
                            ?.image(R.id.iv_right_logo, data?.content)
                            ?.clicked(R.id.iv_right_logo) {
                                data?.content?.let { it1 -> activity?.imgPathPreview(it1) }
                            }
                })
                .register<SlimEditHtmlBean>(R.layout.list_item_177, SlimInjector<SlimEditHtmlBean> { data, injector ->
                    injector?.text(R.id.tv_label, data?.title)
                            ?.with<RichEditor>(R.id.richEditor) {
                                val html = data?.content
                                        ?.replace("<pre>", "")
                                        ?.replace("</pre>", "")
                                it.setPadding(10, 0, 10, 0)
                                it.html = html
                            }
                })
                .attachTo(view.recyclerView)
                .updateData(slimEditBeans as List<*>?)
    }

}